/**
 * Company : Thai Informatic System Co., Ltd.
 * Created By : Dr.Tree
 * Created On : Apr 24, 2012  10:53:57 AM 
 * e-Mail : chatree@tis.co.th
 * Mobile Phone : (+66) 087-921-5115
 * Package Name : sap.business.mm.outbound
 * File Name : GoodsReceiptCheckded.java
 * TODO
 */
package sap.business.mm.outbound;

import java.util.ArrayList;
import java.util.List;
import jpa.entitys.promis.construction.GoodsReceivedNote;
import jpa.entitys.promis.construction.GoodsReceivedNotePK;
import jpa.entitys.synchro.staging.MmGoodsreceiptOutb;
import jpa.service.promis.construction.GoodsReceivedNoteServices;
import jpa.service.synchro.staging.MmGoodsreceiptOutbServices;
import org.apache.log4j.Logger;
import promis.utility.CommonService;
import sap.business.commons.SynchroOutAbstract;
import sap.jco.client.Connection;
import com.sap.conn.jco.JCoFunction;
import com.sap.conn.jco.JCoStructure;
import com.sap.conn.jco.JCoTable;

/**
 * @author chatree
 *
 */
public class GoodsReceiptCheckded extends SynchroOutAbstract {
		
	private static final Logger log = Logger.getLogger(GoodsReceiptCheckded.class);
	
	public GoodsReceiptCheckded() {
		try {
			
			super.emSynchro = super.CreateEmSynchro();
			super.emPromis = super.CreateEmSynchro();
			
		} catch (Exception e) {
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
	}
	
	//Check Purchase Order is LIV By PO Line
	//Dr.Tree or Palith
	//201-04-25 
	private String IsCheckedCancelGr(String sap_po_document_no, String po_item_line_seq, String supplier_code, String po_date) throws Exception {
		this.sap_bapi = "MM_PO_GET_INVOICE";
		this.jco_conn  = new Connection();
		
		try{
			
			if(!CommonService.IsNull(sap_po_document_no) && !CommonService.IsNull(po_item_line_seq) && !CommonService.IsNull(supplier_code) && !CommonService.IsNull(po_date))
			{
				//Get SAP BAPI function
				JCoFunction jcoFunction = jco_conn.getJCoFunction(sap_bapi);
				
				//jcoFunction.getImportParameterList().setValue("PURCHASEORDER", "1410000057");
				//jcoFunction.getImportParameterList().setValue("PO_ITEM", "1");
				//jcoFunction.getImportParameterList().setValue("VENDOR_ID", "0000100003");
				//jcoFunction.getImportParameterList().setValue("FROM_DATE", "20120413");
				//jcoFunction.getImportParameterList().setValue("TO_DATE", "20991231");
				
				jcoFunction.getImportParameterList().setValue("PURCHASEORDER", sap_po_document_no);
				jcoFunction.getImportParameterList().setValue("PO_ITEM", po_item_line_seq);
				jcoFunction.getImportParameterList().setValue("VENDOR_ID", supplier_code);
				jcoFunction.getImportParameterList().setValue("FROM_DATE", po_date);
				jcoFunction.getImportParameterList().setValue("TO_DATE", "20991231"); //Palith Fix TO_DATE 
				
				JCoStructure jcoStru_IVD_STATUS = jco_conn.getImportStructure("IVD_STATUS");
				
				JCoStructure jcoStru_POSTED = jcoStru_IVD_STATUS.getStructure("POSTED");
				jcoStru_POSTED.setValue("BAPIFLAG", "X");		
				jcoStru_IVD_STATUS.setValue("POSTED", jcoStru_POSTED);
				
				//===============================================================================
				JCoStructure jcoStru_IVD_IVD_TYPE = jco_conn.getImportStructure("IVD_TYPE");
				
				JCoStructure jcoStru_INVOICE = jcoStru_IVD_IVD_TYPE.getStructure("INVOICE");
				jcoStru_INVOICE.setValue("BAPIFLAG", "X");
				jcoStru_IVD_IVD_TYPE.setValue("INVOICE", jcoStru_INVOICE);
				
				JCoStructure jcoStru_CREDIT_MEMO = jcoStru_IVD_IVD_TYPE.getStructure("CREDIT_MEMO");
				jcoStru_INVOICE.setValue("BAPIFLAG", "X");
				jcoStru_IVD_IVD_TYPE.setValue("CREDIT_MEMO", jcoStru_CREDIT_MEMO);
				
				JCoStructure jcoStru_SUBSEQUENT_DEBIT = jcoStru_IVD_IVD_TYPE.getStructure("SUBSEQUENT_DEBIT");
				jcoStru_INVOICE.setValue("BAPIFLAG", "X");
				jcoStru_IVD_IVD_TYPE.setValue("SUBSEQUENT_DEBIT", jcoStru_SUBSEQUENT_DEBIT);
				
				JCoStructure jcoStru_SUBSEQUENT_CREDIT = jcoStru_IVD_IVD_TYPE.getStructure("SUBSEQUENT_CREDIT");
				jcoStru_INVOICE.setValue("BAPIFLAG", "X");
				jcoStru_IVD_IVD_TYPE.setValue("SUBSEQUENT_CREDIT", jcoStru_SUBSEQUENT_CREDIT);
				
				jcoFunction.execute(jco_conn.getJCoConnection());
				
				String sap_Liv_structure = "IVD_TAB";
				JCoTable jcotable = jco_conn.getTables(sap_Liv_structure);		
				if(jcotable != null && jcotable.isEmpty() == false)
				{
					log.info("table "+ sap_Liv_structure +" Retrun getNumRows = " + jcotable.getNumRows());
					for(int i = 0; i < jcotable.getNumRows(); i++)
					{
						jcotable.setRow(i);			
						log.info(jcotable.getString("INV_DOC_NO"));
						
						if(!CommonService.IsNull(jcotable.getString("INV_DOC_NO")))
							return CommonService.IsNullRNull(jcotable.getString("INV_DOC_NO"));
					}
				}else 
					log.info(sap_Liv_structure + " IS isEmpty.");
			}
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}
		return null;
	}
	
	private void UpdatedIsCancelToPROMIS_GR(MmGoodsreceiptOutb value, boolean isChk) throws Exception {
		log.info(" Start UpdatedIsCancelToPROMIS_GR. ");
		try{
			
			GoodsReceivedNoteServices GoodsReceivedNoteServ = new GoodsReceivedNoteServices(emPromis);
			
			GoodsReceivedNotePK id = new GoodsReceivedNotePK();
			id.setCompany(value.getCompany());
			id.setProject(value.getProject());
			id.setGrnSeries(value.getGrnSeries());
			id.setGrnType(value.getGrnType());
			id.setGrnNo(value.getGrnNo());
			
			GoodsReceivedNote goodsReceivedNote = GoodsReceivedNoteServ.findById(id);
			if(isChk)
				goodsReceivedNote.setSyncIsCancel("Y");
			else
				goodsReceivedNote.setSyncIsCancel("N");
			
			GoodsReceivedNoteServ.Updated(goodsReceivedNote);
			
		}catch(Exception e)
		{
			log.error(e.getMessage());
			System.err.println(e.getMessage());
			throw e;
		}
	}
	
	//
	private void IsCancelGr(String instanceNo) throws Exception {
		
		boolean isChk = true;
		List<MmGoodsreceiptOutb> vlauelist = new ArrayList<MmGoodsreceiptOutb>(); 
		try{
			
			instanceNo = "10000000000000002618";			
			MmGoodsreceiptOutbServices mmGoodsreceiptOutbServ = new MmGoodsreceiptOutbServices(this.emSynchro);
			
			vlauelist = mmGoodsreceiptOutbServ.findByInstanceNo(instanceNo);
			
			for(MmGoodsreceiptOutb value : vlauelist){
				
				String sap_liv_no = IsCheckedCancelGr(value.getExternalPoNo() , value.getExternalPoLineItem() , value.getSupplierCode() , value.getPoDate());				
				
				value.setSapLivNo(sap_liv_no);
				mmGoodsreceiptOutbServ.Updated(value);
				
				if(isChk == true && CommonService.IsNull(sap_liv_no) == false)
					isChk = false;
			}
			
			if(isChk)
				UpdatedIsCancelToPROMIS_GR(vlauelist.get(0), isChk);
			else
				log.info("Can not Cancel GR :" + vlauelist.get(0).getPromisGrnNo());
			
		}catch(Exception e){
			log.error(e.getMessage());
			System.err.println(e.getMessage());
		}		
		
	}
	
	
	@Deprecated
	private void Start() throws Exception
	{
		this.sap_bapi = "MM_PO_GET_INVOICE";
		this.jco_conn  = new Connection();
		
		//Get SAP BAPI function
		JCoFunction jcoFunction = jco_conn.getJCoFunction(sap_bapi);
		
		jcoFunction.getImportParameterList().setValue("PURCHASEORDER", "1410000057");
		jcoFunction.getImportParameterList().setValue("PO_ITEM", "1");
		jcoFunction.getImportParameterList().setValue("VENDOR_ID", "0000100003");
		jcoFunction.getImportParameterList().setValue("FROM_DATE", "20120413");
		jcoFunction.getImportParameterList().setValue("TO_DATE", "20991231");
		
		JCoStructure jcoStru_IVD_STATUS = jco_conn.getImportStructure("IVD_STATUS");
		
		JCoStructure jcoStru_POSTED = jcoStru_IVD_STATUS.getStructure("POSTED");
		jcoStru_POSTED.setValue("BAPIFLAG", "X");		
		jcoStru_IVD_STATUS.setValue("POSTED", jcoStru_POSTED);
		
		//===============================================================================
		JCoStructure jcoStru_IVD_IVD_TYPE = jco_conn.getImportStructure("IVD_TYPE");
		
		JCoStructure jcoStru_INVOICE = jcoStru_IVD_IVD_TYPE.getStructure("INVOICE");
		jcoStru_INVOICE.setValue("BAPIFLAG", "X");
		jcoStru_IVD_IVD_TYPE.setValue("INVOICE", jcoStru_INVOICE);
		
		JCoStructure jcoStru_CREDIT_MEMO = jcoStru_IVD_IVD_TYPE.getStructure("CREDIT_MEMO");
		jcoStru_INVOICE.setValue("BAPIFLAG", "X");
		jcoStru_IVD_IVD_TYPE.setValue("CREDIT_MEMO", jcoStru_CREDIT_MEMO);
		
		JCoStructure jcoStru_SUBSEQUENT_DEBIT = jcoStru_IVD_IVD_TYPE.getStructure("SUBSEQUENT_DEBIT");
		jcoStru_INVOICE.setValue("BAPIFLAG", "X");
		jcoStru_IVD_IVD_TYPE.setValue("SUBSEQUENT_DEBIT", jcoStru_SUBSEQUENT_DEBIT);
		
		JCoStructure jcoStru_SUBSEQUENT_CREDIT = jcoStru_IVD_IVD_TYPE.getStructure("SUBSEQUENT_CREDIT");
		jcoStru_INVOICE.setValue("BAPIFLAG", "X");
		jcoStru_IVD_IVD_TYPE.setValue("SUBSEQUENT_CREDIT", jcoStru_SUBSEQUENT_CREDIT);
		
		jcoFunction.execute(jco_conn.getJCoConnection());
		
		String sap_Liv_structure = "IVD_TAB";
		JCoTable jcotable = jco_conn.getTables(sap_Liv_structure);		
		if(jcotable != null && jcotable.isEmpty() == false)
		{
			log.info("table "+ sap_Liv_structure +" Retrun getNumRows = " + jcotable.getNumRows());
			for(int i = 0; i < jcotable.getNumRows(); i++)
			{
				jcotable.setRow(i);			
				log.info(jcotable.getString("INV_DOC_NO"));
			}
		}else 
			log.info(sap_Liv_structure + " IS isEmpty.");
		
		
	}

//	/**
//	 * @param args
//	 */
//	public static void main(String[] args) {
//		// TODO Auto-generated method stub
//		
//		try{
//			
//			GoodsReceiptCheckded obj = new GoodsReceiptCheckded();			
//			
//			obj.IsCancelGr("");
//			
//		}catch(Exception e)
//		{
//			log.error(e);
//			e.printStackTrace();
//		}
//	}

}
